//封装请求的方法,
export function getUrlRules(url) {
	var url_rules = [
					'/game/player/login',				//玩家登陆
					'/game/player/register',			//玩家注册
					'/game/player/sendtelphonecode',	//获取验证码
				];
	for (var i in url_rules) {
	    if (url_rules[i] == url) {
	        return true;
	    }
	}
	return false;
}

export function get(url, params = {}) {
    if (this.$getCookie('user_token') == null && url != '/admin/login/login') {
        this.$notify({ type: 'danger', message: '页面失效，请重新登录！'});
        return false
    }
    
	var res_headers = {};
    res_headers.token = this.$getCookie('user_token') ? this.$getCookie('user_token') : '';
    res_headers['Content-Type'] = 'application/json;charset=UTF-8';
    
    url = this.$url + url
    for (var i in params) {
        if (params[i] == "" && params[i] !== 0) {
            delete params[i];
        }
    }
    return new Promise((resolve, reject) => {
        this.$axios.get(url, {
            headers: res_headers,
            params: params,
            timeout: 30 * 1000
        }).then(response => {
            resolve(response.data)
        }).catch(err => {
			console.log(err);
			this.$notify({ type: 'danger', message: err});
            reject(err)
        })
    })
}

export function post(url, params = {}, responseType = '') {
    if (this.$getCookie('user_token') == null && getUrlRules(url) == false) {
		this.$notify({ type: 'danger', message: '页面失效，请重新登录！'});
        return false;
    }

	var res_headers = {};
    res_headers.token = this.$getCookie('user_token') ? this.$getCookie('user_token') : '';
    if (url.indexOf('uploadFiles') >= 0) {
        res_headers['Content-Type'] = 'multipart/form-data';
    } else {
        //res_headers['Content-Type'] = 'application/x-www-form-urlencoded';
		res_headers['Content-Type'] = 'application/json;charset=UTF-8';//'application/form-data';
    }
    
    for (var i in params) {
        if (params[i] == "" && params[i] !== 0) {
            delete params[i];
        }
    }
	
    url = this.$url + url;
	
    return new Promise((resolve, reject) => {
        this.$axios.post(url, params, {
            headers: res_headers,
            responseType: responseType,
            timeout: 120 * 1000
        }).then(response => {
            resolve(response.data)
        }).catch(err => {
			this.$notify({ type: 'danger', message: err});
            reject(err)
        })
    })
}
